package Testing;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class TestClientConfiguration 
{
	//filename
	private final static String CONFIG_FILENAME = "testing.config";
	
	//light load configuration
	private final String LIGHT_LOAD_CLIENT_START_CONFIG_NAME = "LightLoadClientStart";
	private final int DEFAULT_LIGHT_LOAD_CLIENT_START = 5;
	private int LightLoadClientStart = 0;
	
	private final String LIGHT_LOAD_CLIENT_INCREMENT_CONFIG_NAME = "LightLoadClientIncrement";
	private final int DEFAULT_LIGHT_LOAD_CLIENT_INCREMENT = 5;
	private int LightLoadClientIncrement = 0;
	
	private final String LIGHT_LOAD_CLIENT_MAX_CONFIG_NAME = "LightLoadClientMax";
	private final int DEFAULT_LIGHT_LOAD_CLIENT_MAX = 50;
	private int LightLoadClientMax = 0;
	
	private final String LIGHT_LOAD_SAMPLE_SIZE_CONFIG_NAME = "LightLoadSampleSize";
	private final int DEFAULT_LIGHT_LOAD_SAMPLE_SIZE = 10;
	private int LightLoadSampleSize = 0;
	
	//heavy load configuration
	private final String HEAVY_LOAD_CLIENT_START_CONFIG_NAME = "HeavyLoadClientStart";
	private final int DEFAULT_HEAVY_LOAD_CLIENT_START = 5;
	private int HeavyLoadClientStart = 0;
	
	private final String HEAVY_LOAD_CLIENT_INCREMENT_CONFIG_NAME = "HeavyLoadClientIncrement";
	private final int DEFAULT_HEAVY_LOAD_CLIENT_INCREMENT = 5;
	private int HeavyLoadClientIncrement = 0;
	
	private final String HEAVY_LOAD_CLIENT_MAX_CONFIG_NAME = "HeavyLoadClientMax";
	private final int DEFAULT_HEAVY_LOAD_CLIENT_MAX = 50;
	private int HeavyLoadClientMax = 0;
	
	private final String HEAVY_LOAD_SAMPLE_SIZE_CONFIG_NAME = "HeavyLoadSampleSize";
	private final int DEFAULT_HEAVY_LOAD_SAMPLE_SIZE = 10;
	private int HeavyLoadSampleSize = 0;
	
	public TestClientConfiguration()
	{
		LoadConfig(CONFIG_FILENAME);
	}
	
	public int getLightLoadClientStart()
	{
		return LightLoadClientStart;
	}
	
	public int getLightLoadClientIncrement()
	{
		return LightLoadClientIncrement;
	}
	
	public int getLightLoadClientMax()
	{
		return LightLoadClientMax;
	}
	
	public int getLightLoadSampleSize()
	{
		return LightLoadSampleSize;
	}
	
	public int getHeavyLoadClientStart()
	{
		return HeavyLoadClientStart;
	}
	
	public int getHeavyLoadClientIncrement()
	{
		return HeavyLoadClientIncrement;
	}
	
	public int getHeavyLoadClientMax()
	{
		return HeavyLoadClientMax;
	}
	
	public int getHeavyLoadSampleSize()
	{
		return HeavyLoadSampleSize;
	}
	
	private void LoadConfig(String configFileName)
	{
		File file = new File(configFileName);
		
		if(file.exists())
		{
			try 
			{
				BufferedReader reader = new BufferedReader(new FileReader(file));
				String line = null;
				line = reader.readLine();
				
				//read until the end of the line
				while(line != null)
				{
					//parse the line
					String[] config = line.split(",");
					int configValue = Integer.parseInt(config[1]);
					
					//set the config value
					if(config[0].contentEquals(LIGHT_LOAD_CLIENT_START_CONFIG_NAME))
						LightLoadClientStart = configValue;
					else if(config[0].contentEquals(LIGHT_LOAD_CLIENT_INCREMENT_CONFIG_NAME))
						LightLoadClientIncrement = configValue;
					else if(config[0].contentEquals(LIGHT_LOAD_CLIENT_MAX_CONFIG_NAME))
						LightLoadClientMax = configValue;
					else if(config[0].contentEquals(LIGHT_LOAD_SAMPLE_SIZE_CONFIG_NAME))
						LightLoadSampleSize = configValue;
					else if(config[0].contentEquals(HEAVY_LOAD_CLIENT_START_CONFIG_NAME))
						HeavyLoadClientStart = configValue;
					else if(config[0].contentEquals(HEAVY_LOAD_CLIENT_INCREMENT_CONFIG_NAME))
						HeavyLoadClientIncrement = configValue;
					else if(config[0].contentEquals(HEAVY_LOAD_CLIENT_MAX_CONFIG_NAME))
						HeavyLoadClientMax = configValue;
					else if(config[0].contentEquals(HEAVY_LOAD_SAMPLE_SIZE_CONFIG_NAME))
						HeavyLoadSampleSize = configValue;
					else
					{
						System.out.println("\nUnknown config name: " + config[0] + ". Loading default config.\n");
						LoadDefaultConfig();
						break;
					}
					
					line = reader.readLine();
				}
				
				System.out.println("\nConfig file loaded successfully.\n");
			} 
			catch (Exception e) 
			{
				System.out.println("\nThere was an error loading the config file: " + e.getMessage() + ". Loaded default config.\n");
				LoadDefaultConfig();
			}
		}
		else //can't find the file, so load the default config
		{
			System.out.println("\nCannot locate the config file. Loaded default config.\n");
			LoadDefaultConfig();
		}
	}
	
	private void LoadDefaultConfig()
	{
		//light load config
		LightLoadClientStart = DEFAULT_LIGHT_LOAD_CLIENT_START;
		LightLoadClientIncrement = DEFAULT_LIGHT_LOAD_CLIENT_INCREMENT;
		LightLoadClientMax = DEFAULT_LIGHT_LOAD_CLIENT_MAX;
		LightLoadSampleSize = DEFAULT_LIGHT_LOAD_SAMPLE_SIZE;
		
		//heavy load config
		HeavyLoadClientStart = DEFAULT_HEAVY_LOAD_CLIENT_START;
		HeavyLoadClientIncrement = DEFAULT_HEAVY_LOAD_CLIENT_INCREMENT;
		HeavyLoadClientMax = DEFAULT_HEAVY_LOAD_CLIENT_MAX;
		HeavyLoadSampleSize = DEFAULT_HEAVY_LOAD_SAMPLE_SIZE;
	}
}
